#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

// 定义list_head结构，仅使用next指针实现单链表
struct list_head {
    struct list_head *next;
};

#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != NULL; pos = pos->next)
// 初始化链表头
#define LIST_HEAD_INIT(name) { NULL }

#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

// 初始化链表节点
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = NULL;
}

// 判断链表是否为空
static inline int list_empty(const struct list_head *head)
{
    return head->next == NULL;
}

// 添加新节点到链表尾部
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    struct list_head *ptr = head;
    while (ptr->next != NULL)
        ptr = ptr->next;
    ptr->next = new;
    new->next = NULL;
}

// 从链表中删除节点（需要知道前驱节点）
static inline void list_del(struct list_head *prev, struct list_head *entry)
{
    prev->next = entry->next;
}

// 容器_of宏，用于从包含的list_head获取结构体指针
//#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr, type, member) ({                      \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);        \
    (type *)( (char *)__mptr - offsetof(type,member) );})

// 定义链表元素结构，嵌入list_head
struct list_element {
    int data;
    struct list_head list;
};

// 在链表尾部添加元素
void list_add(struct list_head *head, int data)
{
    struct list_element *element = malloc(sizeof(struct list_element));
    if (!element) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    element->data = data;
    INIT_LIST_HEAD(&element->list);
    list_add_tail(&element->list, head);
}

// 查找元素
struct list_element *list_find(struct list_head *head, int target)
{
    struct list_head *ptr;
    list_for_each(ptr, head) {
        struct list_element *element = container_of(ptr, struct list_element, list);
        if (element->data == target)
            return element;
    }
    return NULL;
}

// 删除元素
void list_remove(struct list_head *head, int target)
{
    struct list_head *ptr, *prev;
    prev = head;
    list_for_each(ptr, head) {
        struct list_element *element = container_of(ptr, struct list_element, list);
        if (element->data == target) {
            list_del(prev, ptr);
            free(element);
            return;
        }
        prev = ptr;
    }
}

// 修改元素
int list_modify(struct list_head *head, int old_val, int new_val)
{
    struct list_element *element = list_find(head, old_val);
    if (element) {
        element->data = new_val;
        return 1; // 修改成功
    }
    return 0; // 未找到元素
}

// 遍历链表
void list_traverse(struct list_head *head)
{
    struct list_head *ptr;
    list_for_each(ptr, head) {
        struct list_element *element = container_of(ptr, struct list_element, list);
        printf("%d ", element->data);
    }
    printf("\n");
}

// 简化的遍历宏
//#define list_for_each(pos, head) \
//    for (pos = (head)->next; pos != NULL; pos = pos->next)

// 示例用法
int main()
{
    LIST_HEAD(my_list);

    // 添加元素
    list_add(&my_list, 10);
    list_add(&my_list, 20);
    list_add(&my_list, 30);

    printf("List elements: ");
    list_traverse(&my_list);

    // 查找元素
    struct list_element *element = list_find(&my_list, 20);
    if (element)
        printf("Found element: %d\n", element->data);
    else
        printf("Element not found\n");

    // 修改元素
    list_modify(&my_list, 20, 25);
    printf("List after modification: ");
    list_traverse(&my_list);

    // 删除元素
    list_remove(&my_list, 30);
    printf("List after removal: ");
    list_traverse(&my_list);

    return 0;
}
